package com.itbaizhan.openhis.controller.doctor;

import cn.hutool.core.bean.BeanUtil;
import com.itbaizhan.openhis.domain.CareHistory;
import com.itbaizhan.openhis.domain.CareOrder;
import com.itbaizhan.openhis.domain.CareOrderItem;
import com.itbaizhan.openhis.dto.PatientDto;
import com.itbaizhan.openhis.service.CareHistoryService;
import com.itbaizhan.openhis.service.PatientService;
import com.itbaizhan.openhis.vo.AjaxResult;
import com.itbaizhan.openhis.vo.DataGridView;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Description 患者库管理控制层
 * @Author lin
 */
@RestController
@RequestMapping("doctor/patient")
public class PatientController {

    @Reference
    private PatientService patientService;

    @Reference
    private CareHistoryService careHistoryService;

    /**
     * 分页查询患者信息
     * @return
     */
    @GetMapping("listPatientForPage")
    public AjaxResult listPatientForPage(PatientDto patientDto) {
        DataGridView dataGridView = patientService.listPatientForPage(patientDto);
        return AjaxResult.success("查询成功",dataGridView.getData(),dataGridView.getTotal());
    }


    /**
     * 根据患者ID查询患者信息
     * @param patientId
     * @return
     */
    @GetMapping("getPatientById/{patientId}")
    public AjaxResult getPatientById(@PathVariable@Validated String patientId) {
        return AjaxResult.success(patientService.getPatientById(patientId));
    }

    /**
     * 根据ID查询患者的档案信息
     * @param patientId
     * @return
     */
    @GetMapping("getPatientFileById/{patientId}")
    public AjaxResult getPatientFileById(@Validated @PathVariable String patientId) {
        return AjaxResult.success(patientService.getPatientFileById(patientId));
    }

    /**
     * 根据患者ID查询患者信息、患者档案、历史病例
     * @param patientId
     * @return
     */
    @GetMapping("getPatientAllMessageByPatientId/{patientId}")
    public AjaxResult getPatientAllMessageByPatientId(@PathVariable String patientId) {
        //查询患者病例信息
        List<CareHistory> careHistoryList = careHistoryService.queryCareHistoryByPatientId(patientId);
        //构造返回的数据对象
        List<Map<String,Object>> res = new ArrayList<>();
        for (CareHistory careHistory : careHistoryList) {
            Map<String, Object> careHistoryMap = BeanUtil.beanToMap(careHistory);
            careHistoryMap.put("careOrders", Collections.EMPTY_LIST);
            List<Map<String, Object>> reCareOrders = new ArrayList<>();
            //根据病例ID查询处方列表
            List<CareOrder> careOrders = careHistoryService.queryCareOrdersByChId(careHistory.getChId());
            for (CareOrder careOrder : careOrders) {
                //处方详情
                Map<String, Object> careOrderMap = BeanUtil.beanToMap(careOrder);
                List<CareOrderItem> careOrderItems = careHistoryService.queryCareOrderItemsByCoId(careOrder.getCoId(),null);
                careOrderMap.put("careOrderItems",careOrderItems);
                reCareOrders.add(careOrderMap);
            }
            careHistoryMap.put("careOrders",reCareOrders);
            res.add(careHistoryMap);
        }
        return AjaxResult.success(res);
    }
}
